package com.ruoyi.web.controller.jl;

import java.util.List;
import java.util.Arrays;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.support.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jl.domain.Robot;
import com.ruoyi.jl.service.IRobotService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 机器人控制 控制层
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Controller
@RequestMapping("/jl/robot")
public class RobotController extends BaseController {
    private String prefix = "jl/robot";
	
	private final IRobotService robotService;

	@Autowired
	public RobotController(IRobotService robotService) {
		this.robotService = robotService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("jl:robot:view")
	@GetMapping
	public String robot(){
	    return prefix + "/robot";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		Robot robot = robotService.getById(id);
		mmap.put("robot", robot);
		return prefix + "/edit";
	}
	
	/**
	 * 查询机器人控制列表
	 */
	@RequiresPermissions("jl:robot:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(Robot robot){
		startPage();
        List<Robot> list = robotService.list(robot);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存机器人控制
	 */
	@RequiresPermissions("jl:robot:add")
	@Log(title = "机器人控制", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Robot robot){
		robotService.save(robot);
		return Result.success();
	}

	/**
	 * 修改保存机器人控制
	 */
	@RequiresPermissions("jl:robot:edit")
	@Log(title = "机器人控制", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Robot robot){
		robotService.updateById(robot);
		return Result.success();
	}
	
	/**
	 * 删除机器人控制
	 */
	@RequiresPermissions("jl:robot:remove")
	@Log(title = "机器人控制", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		robotService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出机器人控制列表
	 */
	@RequiresPermissions("jl:robot:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(Robot robot){
		List<Robot> list = robotService.list(robot);
		ExcelUtil<Robot> util = new ExcelUtil<>(Robot.class);
		util.exportExcel(list, "robot");
		return Result.success();
	}
}
